package com.kgcx.organization.controller;

import com.kgcx.common.annotation.validator.IdValidator;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.common.redis.JsonRedisTemplate;
import com.kgcx.common.redis.RedisCons;
import com.kgcx.common.utils.SmsUtils;
import com.kgcx.common.utils.TokenUtils;
import com.kgcx.organization.domain.Customer;
import com.kgcx.organization.domain.CustomerMsg;
import com.kgcx.organization.service.CustomerMsgService;
import com.kgcx.organization.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(description = "乘客接口", tags = "乘客接口")
@RestController
@RequestMapping("customer")
public class CustomerController extends BaseController {
    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerMsgService customerMsgService;
    @Autowired
    private JsonRedisTemplate jsonRedisTemplate;

    @ApiOperation(value = "查询乘客", httpMethod = "GET", notes = "这是用来查询乘客的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "乘客ID", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "phone", value = "电话", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "token", value = "令牌", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("getCustomer")
    public ReturnData<Customer> getCustomer() {
        Customer customer = customerService.getCustomer(getRequestData());
        return new ReturnData<>(customer);
    }

    @ApiOperation(value = "发送验证码", httpMethod = "GET", notes = "发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("code")
    public ReturnData<String> code(@RequestParam String phone) {
        String code = SmsUtils.sendCode(phone);
        jsonRedisTemplate.opsForValue().set(Customer.SMS_VALIDATE_LOGIN_KEY_PREFIX + phone, code, 600, TimeUnit.SECONDS);
        return new ReturnData<>();
    }

    @ApiOperation(value = "登录与注册", httpMethod = "POST", notes = "登录或者注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wxid", value = "微信端ID", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phone", value = "电话", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "source", value = "来源：1=ios,2=android,3=小程序", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "code", value = "验证码", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "mac", value = "mac", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "imei", value = "imei", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "lat", value = "lat", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "lng", value = "lng", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("login")
    public ReturnData<String> login() {
        RequestData rd = getRequestData();
        if ("15898595302".equals(rd.getString("phone")) && "888888".equals(rd.getString("code"))) {
            //ios测试帐号，不需要比对验证码
        } else {
            Object code = jsonRedisTemplate.opsForValue().get(Customer.SMS_VALIDATE_LOGIN_KEY_PREFIX + rd.getString("phone"));
            if (!ObjectUtils.allNotNull(code, rd.getString("code")) || ObjectUtils.notEqual(code, rd.getString("code"))) {
                return new ReturnData<>("-1", "验证码错误！");
            }
        }
        Long kid = customerService.loginOrRegister(rd).getKid();
        String token = TokenUtils.getToken(kid, TokenUtils.TOKEN_SUBJECT_CUSTOMER);
        jsonRedisTemplate.opsForHash().put(RedisCons.TOKEN, TokenUtils.getTokenKey(kid, TokenUtils.TOKEN_SUBJECT_CUSTOMER), token);
        return new ReturnData<>(token);
    }

    @ApiOperation(value = "查询乘客信息", httpMethod = "GET", notes = "这是用来查询乘客信息的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序,sql格式", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "kid", value = "主键ID", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "nameLike", value = "乘客姓名", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "phoneLike", value = "电话", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "wxId", value = "微信ID", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createTimeBegin", value = "注册时间起", required = false, paramType = "query", dataType = "long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "createTimeEnd", value = "注册时间止", required = false, paramType = "query", dataType = "long", dataTypeClass = Long.class)
    })
    @GetMapping("queryCustomerList")
    public ReturnData<List<Customer>> queryCustomerList() {
        RequestData rd = this.getRequestData();
        List<Customer> customerList = customerService.queryCustomerList(rd);
        return new ReturnData<>(customerList);
    }

    /**
     * 专给监管平台提供查询
     */
    @ApiOperation(value = "监管平台查询乘客信息", httpMethod = "GET", notes = "这是用来给监管平台查询乘客信息的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序,sql格式", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "kid", value = "主键ID", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "name", value = "乘客姓名", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "wxId", value = "微信ID", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("queryCustomerWithPage")
    public ReturnData<List<Customer>> queryCustomerWithPage() {
        RequestData rd = this.getRequestData();
        List<Customer> customerList = customerService.queryCustomerWithPage(rd);
        return new ReturnData<>(customerList);
    }

    @ApiOperation(value = "查询消息", httpMethod = "GET", notes = "查询消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerId", value = "乘客id", required = true, paramType = "query", dataType = "String", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "typeId", value = "消息类型", required = true, paramType = "query", dataType = "String", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页容量", required = false, paramType = "query", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序,sql格式", required = false, paramType = "query", dataType = "String", dataTypeClass = String.class),
    })
    @GetMapping("getCustomerMsgList")
    @IdValidator({"customerId"})
    public ReturnData<List<CustomerMsg>> getCustomerMsgList() {
        return new ReturnData<>(customerMsgService.getCustomerMsgList(this.getRequestData()));
    }

    @ApiOperation(value = "发送消息", httpMethod = "POST", notes = "发送消息")
    @PostMapping("sendCustomerMsg")
    public ReturnData sendCustomerMsg(@RequestBody CustomerMsg customerMsg) {
        customerMsgService.sendCustomerMsg(customerMsg);
        return new ReturnData();
    }


}
